package com.dameng.jianyouquan.interviewer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.base.BaseActivity;
import com.dameng.jianyouquan.bean.ShortPeroidListBean;
import com.dameng.jianyouquan.api.NetWorkManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class InterviewerPublishActivity extends BaseActivity {


    @BindView(R.id.recycleView)
    RecyclerView mRecyclerView;
    @BindView(R.id.recycleView_son)
    RecyclerView mRecycleViewSon;
    @BindView(R.id.rl_short)
    RelativeLayout rlShort;
    @BindView(R.id.rl_long)
    RelativeLayout rlLong;
    @BindView(R.id.btn_next)
    Button btnNext;

    private List<ShortPeroidListBean.DataBean> mPageList = new ArrayList<>();
    private MyPeriodAdapter myShortPeriodAdapter;
    private MySonAdapter mySonAdapter;
    private List<ShortPeroidListBean.DataBean.SunBean> pagelistson = new ArrayList<>();

    private boolean isLongPeriod = false;
    private String industryID;
    private String postID;
    private String industryName;
    private String postName;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_interviewer_publish);
        ButterKnife.bind(this);


        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        myShortPeriodAdapter = new MyPeriodAdapter(mPageList);
        mRecyclerView.setAdapter(myShortPeriodAdapter);

        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        gridLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleViewSon.setLayoutManager(gridLayoutManager2);
        mySonAdapter = new MySonAdapter(pagelistson);
        mRecycleViewSon.setAdapter(mySonAdapter);


        NetWorkManager.getService()
                .getShortPeriodList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShortPeroidListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ShortPeroidListBean bean) {
                        int code = bean.getCode();
                        if (code == 200) {
                            List<ShortPeroidListBean.DataBean> mPageList = bean.getData();
                            myShortPeriodAdapter.setPageList(mPageList);
                        }
                    }
                });

    }

    @OnClick({R.id.rl_short, R.id.rl_long, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_short:
                NetWorkManager.getService()
                        .getShortPeriodList()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<ShortPeroidListBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ShortPeroidListBean bean) {
                                int code = bean.getCode();
                                if (code == 200) {
                                    List<ShortPeroidListBean.DataBean> mPageList = bean.getData();
                                    myShortPeriodAdapter.setPageList(mPageList);
                                    if (mPageList != null && mPageList.size() > 0) {
                                        List<ShortPeroidListBean.DataBean.SunBean> sun = mPageList.get(0).getSun();
                                        mySonAdapter.setSonList(sun);
                                    }
                                }
                            }
                        });
                break;
            case R.id.rl_long:
                NetWorkManager.getService()
                        .getLongPeriodList()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<ShortPeroidListBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ShortPeroidListBean bean) {
                                int code = bean.getCode();
                                if (code == 200) {
                                    List<ShortPeroidListBean.DataBean> mPageList = bean.getData();
                                    myShortPeriodAdapter.setPageList(mPageList);
                                    if (mPageList != null && mPageList.size() > 0) {
                                        List<ShortPeroidListBean.DataBean.SunBean> sun = mPageList.get(0).getSun();
                                        mySonAdapter.setSonList(sun);
                                    }
                                }
                            }
                        });
                break;
            case R.id.btn_next:
                if (TextUtils.isEmpty(industryID) || TextUtils.isEmpty(postID)) {
                    Toast.makeText(getApplicationContext(), "请选择职业", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(InterviewerPublishActivity.this, InterviewerPublish2Activity.class);
                    intent.putExtra("postID", postID);
                    intent.putExtra("postName", postName);
                    intent.putExtra("industryID", industryID);
                    intent.putExtra("industryName", industryName);
                    startActivity(intent);
                }
                break;
        }
    }


    private class MyPeriodAdapter extends RecyclerView.Adapter<MyPeriodHolder> {

        private List<ShortPeroidListBean.DataBean> page = new ArrayList<>();

        private MyPeriodAdapter(List<ShortPeroidListBean.DataBean> mPageList) {
            page = mPageList;
        }

        private void setPageList(List<ShortPeroidListBean.DataBean> list) {
            page = list;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public MyPeriodHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = InterviewerPublishActivity.this.getLayoutInflater().inflate(R.layout.item_profession, null);
            return new MyPeriodHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyPeriodHolder holder, int position) {
            ShortPeroidListBean.DataBean pageBean = page.get(position);
            holder.tv_name.setText(pageBean.getParamName());
            industryID = pageBean.getParamId() + "";
            industryName = pageBean.getParamName();
            holder.itemView.setOnClickListener(view -> {
                List<ShortPeroidListBean.DataBean.SunBean> sun = page.get(position).getSun();
                mySonAdapter.setSonList(sun);
            });
        }

        @Override
        public int getItemCount() {
            if (page == null) {
                return 0;
            }
            return page.size();
        }

    }


    private class MyPeriodHolder extends RecyclerView.ViewHolder {

        TextView tv_name;

        private MyPeriodHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }


    private class MySonAdapter extends RecyclerView.Adapter<MyPeriodSonHolder> {

        private List<ShortPeroidListBean.DataBean.SunBean> sunlist = new ArrayList<>();

        public MySonAdapter(List<ShortPeroidListBean.DataBean.SunBean> pagelistson) {
            sunlist = pagelistson;
        }

        private void setSonList(List<ShortPeroidListBean.DataBean.SunBean> sun) {
            this.sunlist = sun;
            notifyDataSetChanged();
        }


        @NonNull
        @Override
        public MyPeriodSonHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = InterviewerPublishActivity.this.getLayoutInflater().inflate(R.layout.item_profession, null);
            return new MyPeriodSonHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyPeriodSonHolder holder, int position) {
            ShortPeroidListBean.DataBean.SunBean sunBean = sunlist.get(position);
            holder.tv_name.setText(sunBean.getParamName());
            postID = sunBean.getParamId() + "";
            postName = sunBean.getParamName();
        }

        @Override
        public int getItemCount() {
            return sunlist.size();
        }


    }


    private class MyPeriodSonHolder extends RecyclerView.ViewHolder {

        TextView tv_name;

        private MyPeriodSonHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }

}
